/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.widgets;

import java.util.Iterator;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

/**
 * Creates a standard GroupBox to surround other elements.<br/>
 * This widget uses two style classes. One for the groupbox itself
 * (which is in fact a fieldset element) and another for the caption
 * (which is a legend element).<br/>
 * The groupbox itself uses {@code biz_eGroupBox}<br/>
 * The caption uses {@code biz_eGroupBoxCaption}.<br/>
 * The groupbox style can be changed using the standard methods. To
 * change the caption style use the {@code set-} and {@code addCaptionStyle}
 * methods.
 * 
 * @author Joost A. Bloemsma
 * 
 */
public class GroupBox extends Composite implements HasWidgets {
    private final FieldSetPanel pnl;
    private final Element legend;

    /**
     * Constructs a new GroupBox instance
     */
    public GroupBox() {
        pnl = new FieldSetPanel();
        this.initWidget(pnl);
        pnl.setStyleName("biz_eGroupBox");
        this.legend = DOM.createLegend();
        pnl.getElement().appendChild(legend);
        legend.setClassName("biz_eGroupBoxCaption");
    }

    /**
     * Constructs a new GroupBox instance
     * 
     * @param caption
     */
    public GroupBox(String caption) {
        this();
        this.setCaption(caption);
    }

    /**
     * @param style
     */
    public final void addCaptionStyle(String style) {
        legend.addClassName(style);
    }

    /**
     * @param style
     */
    public final void setCaptionStyle(String style) {
        legend.setClassName(style);
    }

    /**
     * @return the caption of the groupbox
     */
    public String getCaption() {
        return legend.getInnerText();
    }

    /**
     * Sets the caption of the groupbox
     * 
     * @param caption
     */
    public void setCaption(String caption) {
        legend.setInnerText(caption);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear() {
        pnl.clear();
        // Add the legend node again
        pnl.getElement().appendChild(legend);
    }

    private class FieldSetPanel extends AbsolutePanel {
        FieldSetPanel() {
            super(DOM.createFieldSet());
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void add(Widget w) {
        pnl.add(w);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterator<Widget> iterator() {
        return pnl.iterator();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean remove(Widget w) {
        return pnl.remove(w);
    }
}
